package com.slipper.system.dictionary.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.core.cache.CacheKey;
import com.slipper.core.cache.CacheUtil;
import com.slipper.core.message.MessageUtil;
import com.slipper.core.message.Topic;
import com.slipper.exception.CommonException;
import com.slipper.system.dictionary.bo.DictionaryBO;
import com.slipper.system.dictionary.bo.DictionaryDataBO;
import com.slipper.system.dictionary.dao.DictionaryDao;
import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.system.dictionary.dao.DictionaryDataDao;
import com.slipper.util.Tool;
import com.slipper.util.UUID;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@ReadTransactional
public class DictionaryService {
    @Resource
    private DictionaryDao dictionaryDao;
    @Resource
    private DictionaryDataDao dictionaryDataDao;

    public DictionaryBO queryBy(String id) {
        return dictionaryDao.queryBy(id);
    }

    public PageInfo<DictionaryBO> queryPage(DictionaryBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dictionaryDao.queryList(bo));
    }
    
    @WriteTransactional
    public Integer save(DictionaryBO bo){
        int result;
        if (bo.getId() != null && !"".equals(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            result = this.dictionaryDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            result = this.dictionaryDao.insert(bo);
        }
        dictionaryDataDao.updateDictName(bo.getId(), bo.getName(), System.currentTimeMillis());
        CacheUtil.remove(CacheKey.Dictionary.KEY);
        MessageUtil.publish(Topic.DICTIONARY_CHANGE, null);
        return result;
    }

    @WriteTransactional
    public void saveList(List<DictionaryBO> dictionaryList) {
        if (dictionaryList == null) return;

        dictionaryList.forEach(bo -> {
            if (dictionaryDao.queryBy(bo.getId()) != null) {
                dictionaryDao.update(bo);
            } else {
                dictionaryDao.insert(bo);
            }
            dictionaryDataDao.updateDictName(bo.getId(), bo.getName(), System.currentTimeMillis());
        });
        CacheUtil.remove(CacheKey.Dictionary.KEY);
        MessageUtil.publish(Topic.DICTIONARY_CHANGE, null);
    }
    
    @WriteTransactional
    public Integer delete(DictionaryBO bo) {
        List<String> idList = new ArrayList<>();
        if (Tool.isNotBlank(bo.getId())) {
            idList.add(bo.getId());
        }
        if (bo.getIds() != null) {
            for (String id : bo.getIds()) {
                if (Tool.isNotBlank(id)) {
                    idList.add(id);
                }
            }
        }
        DictionaryDataBO qo = new DictionaryDataBO();
        qo.setDictIds(idList.toArray(new String[0]));
        if (!dictionaryDataDao.queryList(qo).isEmpty()) throw new CommonException("请先删除字典数据！");
        int result = dictionaryDao.delete(bo);
        CacheUtil.remove(CacheKey.Dictionary.KEY);
        MessageUtil.publish(Topic.DICTIONARY_CHANGE, null);
        return result;
    }

    public List<DictionaryBO> queryList(DictionaryBO bo) {
        return this.dictionaryDao.queryList(bo);
    }

}
